package com.lanou3g.baidumusic.main.musicfragment.songlistfragment;


import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lanou3g.baidumusic.R;
import com.lanou3g.baidumusic.main.mainactivity.BaseFragment;
import com.lanou3g.baidumusic.main.tools.NetResultInterface;
import com.lanou3g.baidumusic.main.tools.NetWorkInstance;
import com.lanou3g.baidumusic.main.tools.RecyclerViewOnClickListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class SongListFragment extends BaseFragment implements RecyclerViewOnClickListener{

    private ImageView downIv ;
    private TextView hotTv ;
    private TextView newTv ;
    private RecyclerView recyclerView ;
    private SongListAdapter adapter ;

    private FragmentTransaction transaction ;
    private SongListBean bean ;



    private String  urlStrsHot =
            "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.9.9.3&channel=1413b&operator=1&method=baidu.ting.ugcdiy.getChanneldiy&param=2BNcwVMnUyMwJC3alKtWDnug%2FP3sp3lfoqQh5pVN%2BW1aT3yn8KBqBA%2Bsw27qjaW9ZvMfXQs2DE8rkjNxQNPwfh%2BbJgJRdgL9C444nBoTVuoVdiX5EWcf7bFo27swso37&timestamp=1489739585&sign=0634e52f9d0a0d9ae46a821a9a40dc7a";
    private String  urlStrsNew =
            "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.9.9.3&channel=1413b&operator=1&method=baidu.ting.ugcdiy.getChanneldiy&param=1YPGxjpRnO7%2B1CPE1osmZ4kjakdY70lVgqoE9PIQCWNXNf%2F1xAmPCekA2fsNZlHiAkCLzuKY%2BIUkDM78PZkLMopXmDvSB%2BqV2z%2FrhsMyQ8OSHX1Lgs96Bpy6UqICPz%2FS&timestamp=1490085894&sign=4a9e192c30c22080cb5e63a9917e662a";


    public SongListFragment() {
        // Required empty public constructor
    }


    @Override
    public int setLayout() {
        return R.layout.fragment_song_list;
    }

    @Override
    public void initView(View view) {

        downIv = (ImageView) view.findViewById(R.id.fragment_song_list_down_iv);
        hotTv = (TextView) view.findViewById(R.id.fragment_song_list_hot_tv);
        newTv = (TextView) view.findViewById(R.id.fragment_song_list_new_tv);
        recyclerView = (RecyclerView) view.findViewById(R.id.fragment_song_list_rv_rv);
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext() , 2 ,LinearLayoutManager.VERTICAL,false) ;
        recyclerView.setLayoutManager(gridLayoutManager);




    }

    @Override
    public void initData() {

        adapter = new SongListAdapter(getContext());

        recyclerView.setAdapter(adapter);


        getHotData();


        //左边最热
        hotTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getHotData();
                //返回顶部
                recyclerView.scrollToPosition(0);
            }
        });

        //右边最新
        newTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getNewData();
                recyclerView.smoothScrollToPosition(0);

            }
        });

        //左上角点击弹出popupWindow
        downIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });



    }



    //左边最热数据
    private void getHotData() {
        NetWorkInstance.getInstance().netRequest(urlStrsHot, new NetResultInterface() {
            //当网络请求成功了,自动调用此方法
            @Override
            public void sucess(String str) {
//                Log.d("success", str);

                //gson解析
                Gson gson = new Gson();
                //解析后的数据存于homeEntity中
                SongListBean songListBean = gson.fromJson(str , SongListBean.class);
//                homeEntity.getData().getItems().get(1).getIntroduction();
                adapter.setSongListBean(songListBean);

            }
            //网络请求失败了,自动调用次方法
            @Override
            public void faile(String str) {
                Log.d("faile" + "", str);
            }
        });
    }
    //右边最新数据
    private void getNewData() {
        NetWorkInstance.getInstance().netRequest(urlStrsNew, new NetResultInterface() {
            //当网络请求成功了,自动调用此方法
            @Override
            public void sucess(String str) {
//                Log.d("success", str);

                //gson解析
                Gson gson = new Gson();
                //解析后的数据存于homeEntity中
                SongListBean songListBean = gson.fromJson(str , SongListBean.class);
//                homeEntity.getData().getItems().get(1).getIntroduction();
                adapter.setSongListBean(songListBean);

            }
            //网络请求失败了,自动调用次方法
            @Override
            public void faile(String str) {
                Log.d("faile" + "", str);
            }
        });
    }



    @Override
    public void onItemClick(int position) {

        Intent intent = new Intent("123");
        intent.putExtra("001" , bean.getDiyInfo().get(position).getList_id().toString());
        context.sendBroadcast(intent);
        String s = intent.getStringExtra("001");

    }
}
